<template>
<!-- 特殊评分项规则组件 -->
  <div>
    <el-table
      :data="tableData"
      style="width: 100%" 
      height="505px"
      ref="multipleTable"
      highlight-current-row
      border
      header-cell-class-name="table-header-class">

      <el-table-column
        label="特殊规则名称"
        align='center'
        width="180">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remark" placeholder="请输入" :disabled="pageDisabled"></el-input>
        </template>
      </el-table-column>

      <el-table-column
        label="评点对象.命中标记"
        align='center'
        min-width="240">
        <template slot-scope="scope">
          <el-tooltip :disabled="!scope.row.hitMark" effect="dark" :content="handleResult(scope.row.hitMark)" placement="top">
            <span :class="['result-item',{'disabled-item': pageDisabled}]" @click="measuringResultClick(scope.row,'评点对象.命中标记')">{{handleResult(scope.row.hitMark)}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        label="评点对象.评分项"
        align='center'
        width="180">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.scoringItems" :precision="0" placeholder="请输入" :disabled="pageDisabled"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column
        label="评点对象.累加标记"
        align='center'
        width="180">
        <template slot-scope="scope">
          <el-select v-model="scope.row.scoringItemsTag" placeholder="请选择" clearable :disabled="pageDisabled">
            <el-option
              v-for="item in yesOrNoDic"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作" align='center' width="150" fixed="right" v-if="!pageDisabled">
        <template slot-scope="scope">
          <div style="display:flex;justify-content: center;">
            <span class="handle-btn" @click="handleAdd(scope.$index, scope.row)">新增</span>
            <span class="handle-btn" @click="handleDel(scope.$index, scope.row)">删除</span>
            <span class="handle-btn" @click="handleCopy(scope.$index, scope.row)">复制</span>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <!-- 新增疾病规则弹窗 -->
    <el-dialog
      v-if="conditionVisible"
      title="配置条件"
      :visible.sync="conditionVisible"
      width="850px"
      :close-on-click-modal="false"
      center>
      <conditionRelation ref="conditionRelation" :tableTitle="tableTitle" source="special" :logicDic="logicDic" @saveRelation="()=>{conditionVisible = false}"></conditionRelation>
    </el-dialog>
  </div>
</template>

<script>
  import conditionRelation from "./conditionRelation";

  import constant from '@/utils/constant';
  import {
    getSpecialScoreRuleApi
  } from "@/api/medicalLibrary/examProjectLibrary";
  export default {
    components: {
	  	conditionRelation
	  },
    props: {
      logicDic: {
        type: Array,
        default: ()=>{
          return []
        }
      },
      pageDisabled: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        yesOrNoDic: [{value: '0', label: '否'},{value: '1', label: '是'}],  //是否字典表 
        tableData: [],
        ruleGroupVersionId: '',  //版本id

        // 弹窗
        conditionVisible: false,
        tableTitle: '',  //表头名称
      }
    },
    methods: {
      // 获取疾病的特殊评分规则
      getRiskRule(diseaseID,version){
        this.$store.dispatch('loading/CHANGE_LOADING',true)
        this.tableData = []
        getSpecialScoreRuleApi({diseaseID,version}).then(res => {
					this.$store.dispatch('loading/CHANGE_LOADING',false)
					if (res.resultCode == constant.SUCCESS) {
						this.tableData = res.data || []

            let coverFlag = true
            if(!this.tableData.length) {
              if(!this.pageDisabled) this.handleAdd(-1)
            } else {
              this.ruleGroupVersionId = res.data[0].ruleGroupVersionId
              coverFlag = false
              this.tableData.forEach((item,index)=>{
                if(!item.scoringItems && item.scoringItems!==0) item.scoringItems = undefined
              })
            }
            this.$emit('getCoverFlag',coverFlag,this.ruleGroupVersionId)
					}
				}).catch(err => {
					this.$store.dispatch('loading/CHANGE_LOADING',false);
				});
      },
      // 点击测量结果
      measuringResultClick(row,tableTitle){
        if(this.pageDisabled) return
        this.tableTitle = tableTitle
        this.conditionVisible = true
        setTimeout(()=>{
          // 处理配置条件列表数据
          this.$refs.conditionRelation.currentRow = row
          this.$refs.conditionRelation.handleRelation('hitMark')
        })
      },
      // 处理测量结果的显示
      handleResult(measuringResult){
        let result = measuringResult
        if(result) {
          result = result.replace(/\^/g, '').replace(/#/g, '').replace(/\$/g, '').replace(/\("/g, '').replace(/\"\)/g, '')
        }
        return result
      },

      /* ----列表操作按钮 */
      // 添加
      handleAdd(index, row) {
        let obj = {
          hitMark: '',
          scoringItems: undefined,
          scoringItemsTag: ''
        }
        // this.tableData.splice(index+1,0,obj)
        this.tableData.push(obj)
      },
      // 删除
      handleDel(index, row) {
        this.tableData.splice(index,1)
        if(!this.tableData.length) this.handleAdd(-1)
      },
      // 复制
      handleCopy(index, row) {
        let fields = ['hitMark','scoringItems','scoringItemsTag']
        let obj = {}
        fields.forEach(item=>{
          obj[item] = row[item]
        })
        this.tableData.push(obj)
        // this.tableData.splice(index+1,0,obj)
      },

      // 处理保存的数据
      saveHandle(){
        let riskRuleFieds = ['hitMark','scoringItems','scoringItemsTag'],
        flag = true
        let filterArr = JSON.parse(JSON.stringify(this.tableData))
        filterArr.forEach(fItem=>{
          let itemFlag = false
          if(this.ruleGroupVersionId) fItem.ruleGroupVersionId = this.ruleGroupVersionId
          if(!fItem.scoringItems && fItem.scoringItems !== 0) fItem.scoringItems = ''
          
          riskRuleFieds.forEach(item=>{
            if(fItem[item] || fItem[item] === 0) itemFlag = true
          })
          if(!itemFlag) flag = false
        })
        return {
          flag,
          filterArr
        }
      }
    }
  }
</script>
<style lang="less" scoped>
.handle-btn{
  cursor: pointer;
  color: #409eff;
  display: inline-block;
  padding: 0 8px;
}
.result-item{
  color: #40a9fd;
  cursor: pointer;
  width: 100%;
  display: inline-block;
  height: 24px;
  line-height: 30px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.disabled-item{
  color: #606266;
  cursor: no-drop;
}

/deep/ .el-input-number__decrease,/deep/ .el-input-number__increase{
  display: none;
}
</style>